//
//  FSViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/8/2.
//  Copyright © 2016年 ZhiYu Hao. All rights reserved.
//

#import "FSViewController.h"
#import "ZiLiaoTableViewCell.h"
#import "IntroViewController.h"
#import "IGGetFansMemberApi.h"
#import "IGFocusmember.h"
#import "IGArtGalleryAttentApi.h"
@interface FSViewController () <UITableViewDelegate, UITableViewDataSource>
{
    NSString *followerId;
    UILabel *msg;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic,strong)IGGetFansMemberApi *getFansApi;
@property (nonatomic, strong) IGArtGalleryAttentApi *attentApi;
@property (nonatomic, copy) NSString *pageNum;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation FSViewController

-(NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.table.backgroundColor = [UIColor c2Color];
    _pageNum = @"1";
    self.getFansApi = [IGGetFansMemberApi new];
    [self.table ig_setHeaderWithRefreshingBlock:^{
        _pageNum = @"1";
        [self dataRequest];
    }];
    
    [self dataRequest];
}

- (void)firstPage
{
    _pageNum = @"1";
    [self dataRequest];
}

-(void)dataRequest
{
    self.getFansApi.pageNum = _pageNum;
    self.getFansApi.uid = _u_id;
    @KWeakObj(self)
    [self.getFansApi startWithDecodeClass:[IGGetFansMemberResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [selfWeak.table ig_endHeaderRefreshing];
        [selfWeak.table ig_endFooterRefreshing];
        [selfWeak hidden];
        if (responseObject)
        {
            IGGetFansMemberResp *resp=responseObject;
            IGGetFansMemberModel *model=resp.data;
            if (model.fansMemberList&&model.fansMemberList.count>0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                if ([_pageNum isEqualToString:@"1"] && _dataSource.count < 1)
                {
                    [selfWeak show];
                }
                [selfWeak.table ig_endFooterRefreshingWithNoMoreData];
            }
            
            if ([_pageNum isEqualToString:@"1"])
            {
                _dataSource = [NSMutableArray arrayWithArray:model.fansMemberList];
            }
            else
            {
                [_dataSource addObjectsFromArray:model.fansMemberList];
            }
            [selfWeak.table reloadData];
        }
        else
        {
            if (_dataSource.count < 1)
            {
                [selfWeak show];
            }
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.table ig_setFooterWithRefreshingBlock:^{
        _pageNum = [NSString stringWithFormat:@"%ld", [_pageNum integerValue] + 1];
        [selfWeak dataRequest];
    }];
}

- (void)show
{
    if (!msg) {
        msg = [[UILabel alloc] initWithFrame:CGRectZero];
//        msg.text = @"TA还没有粉丝, 期盼着你的关注...";
        msg.text = NSLocalizedString(@"No Follower", nil);
        msg.textColor = [UIColor c4Color];
        msg.textAlignment = NSTextAlignmentCenter;
        msg.numberOfLines = 0;
    }
    [self.view addSubview:msg];
    msg.width = self.view.width - 40;
    msg.height = self.view.height;
    msg.center = CGPointMake(self.view.width/2, self.view.height/2);
}

- (void)hidden
{
    [msg removeFromSuperview];
}


#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count ? : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    @KWeakObj(self)
    ZiLiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil)
    {
        cell = [[ZiLiaoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.data = _dataSource[indexPath.row];
    cell.follow.hidden = YES;

//    [cell followOwerAction:^(IGFocusmember *info) {
//        if ([info.attentionSate integerValue]==1)
//        {
//            [selfWeak attentDataRequest:@"0" section:indexPath.row];
//        }
//        else
//        {
//            [selfWeak attentDataRequest:@"1" section:indexPath.row];
//        }
//    }];
    [cell setNeedsLayout];
    return cell;
}


//- (void)attentDataRequest:(NSString *)operatorType section:(NSUInteger)section
//{
//    IGFocusmember *mem = _dataSource[section];
//    self.attentApi.operatorType = operatorType;
//    self.attentApi.museumId = mem.otherId;
//    self.attentApi.uId = [IGUser getUid];
//    @KWeakObj(self)
//    [self.attentApi startWithDecodeClass:[IGArtGalleryAttentResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
//        if (responseObject)
//        {
//            NSString *str = [operatorType isEqualToString:@"0"] ? @"0" : @"1";
//            mem.attentionSate = str;
//            [_dataSource replaceObjectAtIndex:section withObject:mem];
//            [selfWeak.table reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
//        }
//    }];
//}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    IGFocusmember *mem = _dataSource[indexPath.row];
    IntroViewController *vc = [[IntroViewController alloc] init];
    vc.u_id = mem.otherId;
    [UIViewController ig_pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
